package biz

import (
	"context"
	"errors"
	"log"
)

var (
	ErrVipGradeInvalid = errors.New("vip Grade invalid")
)

// VipInfo 会员信息
type VipInfo struct {
	VipGrade int32 // 会员等级
	// ......其它可能存在的信息
}

// NewVipInfo vip消息
func NewVipInfo(vipGrade int32) (VipInfo, error) {
	if vipGrade < 0 {
		return VipInfo{}, ErrVipGradeInvalid
	}
	return VipInfo{
		VipGrade: vipGrade,
	}, nil
}

type VipInfoRepo interface {
	Find(ctx context.Context, userID string) (*VipInfo, error)
	Save(ctx context.Context, v *VipInfo) error
	UpdateGrade(ctx context.Context, grade int32, userID string) error
}
type VipInfoUseCase struct {
	repo VipInfoRepo
}

func NewVipInfoUseCase(repo VipInfoRepo) *VipInfoUseCase {
	return &VipInfoUseCase{repo: repo}
}

// GetVipInfo 获取会员信息
func (v *VipInfoUseCase) GetVipInfo(ctx context.Context, userID string) (VipInfo, error) {
	vip, err := v.repo.Find(ctx, userID)
	if err != nil {
		log.Printf("GetVipInfo err %v \n", err)
		if errors.Is(err, ErrUserNotFound) {
			return VipInfo{}, ErrUserNotFound
		}
		return VipInfo{}, ErrBizInternal
	}
	return NewVipInfo(vip.VipGrade)
}
